<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="小说名称" prop="title">
        <el-input v-model="form.title" placeholder="小说名称" disabled />
      </el-form-item>
      <el-form-item label="章节序号" prop="orderNum">
        <el-input v-model="form.orderNum" placeholder="请输入章节序号" disabled />
      </el-form-item>
      <el-form-item label="章节标题" prop="chapterTitle">
        <el-input v-model="form.chapterTitle" placeholder="请输入章节标题" />
      </el-form-item>
      <el-form-item label="章节内容" prop="content">
        <el-input
          type="textarea"
          v-model="form.content"
          :rows="15"
          placeholder="请输入章节内容"
          @input="updateWordCount"
        />
        <div class="word-count">字数：{{ form.wordCount }}</div>
      </el-form-item>
      <el-form-item label="订阅状态" prop="subscribe">
        <el-radio-group v-model="form.subscribe">
          <el-radio :label="0">免费</el-radio>
          <el-radio :label="1">VIP</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { updateChapter } from "@/api/writer/writer";

export default {
  name: "UpdateChapter",
  data() {
    return {
      // 表单参数
      form: {
        title: "",
        novelId: "",
        orderNum: "",
        chapterTitle: "",
        content: "",
        subscribe: 0,
        wordCount: "",
        createTime: ""
      },
      // 表单校验
      rules: {
        orderNum: [
          { required: true, message: "章节序号不能为空", trigger: "blur" }
        ],
        chapterTitle: [
          { required: true, message: "章节标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "章节内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // 从路由参数中获取小说标题和章节信息
    const { title, orderNum, chapterTitle, content, subscribe } = this.$route.query;
    this.form.title = title;
    this.form.orderNum = orderNum;
    this.form.chapterTitle = chapterTitle;
    this.form.content = content;
    this.form.subscribe = parseInt(subscribe) || 0;
    // 计算字数
    this.form.wordCount = content ? content.length.toString() : "0";
    // 设置创建时间，格式化为"YYYY-MM-DD HH:mm:ss"
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    this.form.createTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 更新字数统计
          this.form.wordCount = this.form.content.length.toString();
          updateChapter(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.$router.push({
        path: '/writer/novelDetail',
        query: {
          title: this.form.title
        }
      });
    },
    // 更新字数统计
    updateWordCount() {
      this.form.wordCount = this.form.content.length.toString();
    }
  }
};
</script>

<style scoped>
.word-count {
  margin-top: 8px;
  color: #666;
  font-size: 14px;
  text-align: right;
}
</style> 